<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/18
 * Time: 9:53
 */

namespace app\admin\controller;

use app\model\WithdrawLog as ModelWithdrawLog;
use app\logic\Excel as MobileLogicExcel;
use app\model\ApplyForSettled as ModelApplyForSettled;
use app\model\DepositReturnLog as ModelDepositReturnLog;


class Financial extends Member
{
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 提现列表
     * uri:/admin/withdrawal.list
     */
    public function withdrawalList(){
        $orderSn = $this->_getContentParams('orderSn');
        $scope = $this->_getContentParams('scope');
        $int_now_page = $this->_getContentParams('page');$int_now_page=1; // 当前页码
        _toValidate('id', $int_now_page, 10005);

        $int_list_rows = $this->_getContentParams('listRows');$int_list_rows=10; // 一页的条数
        _toValidate('id', $int_list_rows, 10006);
        $sorting = $this->_getContentParams('sorting');
        if (empty($sorting)){
            $sorting="desc";
        }
        $arr_where='';
        switch ($scope){
            case 1://100元以下
                $arr_where.="money<=100";
                break;
            case 2://100-200
                $arr_where.="money between 100 and 200";
                break;
            case 3://200-500
                $arr_where.="money between 200 and 500";
                break;
            case 4://500-1000
                $arr_where.="money between 500 and 1000";
                break;
            case 5://1000以上
                $arr_where.="money>1000 ";
                break;
            default:
                break;
        }
        if (!empty($scope) and !empty($orderSn)){
            $arr_where.=" and withdraw_number like %$orderSn%";
        }elseif(!empty($orderSn)){
            $arr_where.="withdraw_number like %$orderSn%";
        }
        $int_total_rows = ModelWithdrawLog::getInstance()->toCount($arr_where);
        $int_total_pages = (int)ceil($int_total_rows / $int_list_rows);//总页数
        _toCompare(($int_now_page > $int_total_pages && $int_total_pages > 0), '===', true, 10007);
        $int_first_row = $int_list_rows * ($int_now_page - 1);//从第几条开始查询

        $data = ModelWithdrawLog::getInstance()->toSelect($arr_where,"id,withdraw_number AS withdrawNumber,real_name AS realName,money,state,gmt_create AS gmtCreate",$int_first_row,$int_list_rows,"state $sorting,gmt_create $sorting");
        _returnData(['totalPages' => $int_total_pages,'withdrawalList'=>$data]);
    }
    /**
     * 提现列表导出
     * uri:/admin/withdrawal.listExport
     */
    public function withdrawalListExport(){
        $orderSn = $this->_getContentParams('orderSn');
        $scope = $this->_getContentParams('scope');
        $sorting = $this->_getContentParams('sorting');
        if (empty($sorting)){
            $sorting="desc";
        }
        $str_excel = $this->_getContentParams('excel');
        $type = $this->_getContentParams('type');
        $arr_where='';
        if ($type==1){
            switch ($scope){
                case 1://100元以下
                    $arr_where.="money<=100";
                    break;
                case 2://100-200
                    $arr_where.="money between 100 and 200";
                    break;
                case 3://200-500
                    $arr_where.="money between 200 and 500";
                    break;
                case 4://500-1000
                    $arr_where.="money between 500 and 1000";
                    break;
                case 5://1000以上
                    $arr_where.="money>1000";
                    break;
                default:
                    break;
            }
            if (!empty($scope) and !empty($orderSn)){
                $arr_where.=" and withdraw_number like %$orderSn%";
            }elseif(!empty($orderSn)){
                $arr_where.="withdraw_number like %$orderSn%";
            }
        }elseif ($type==2){
            $arrId = $this->_getContentParams('arrId');
            $arr_where.="id in ($arrId)";
        }

        $list = ModelWithdrawLog::getInstance()->toSelect($arr_where,"id,withdraw_number AS withdrawNumber,real_name AS realName,money,state,gmt_create AS gmtCreate",'','',"state $sorting,gmt_create $sorting");
        if($str_excel == 'excel'){
            MobileLogicExcel::getInstance()->export($list, ['id' => '提现记录ID', 'withdrawNumber' => '订单编号', 'realName' => '用户名称','money'=>'提现金额','state'=>'提现状态（1：提现中；2：提现完成；3:提现失败）','gmtCreate'=>'提现时间'], '提现列表'.date('Y-m-d', time()));
        }
        _returnData();
    }

    /**
     * 提现详情
     * uri:/admin/withdrawal.details
     */
    public function withdrawalDetails(){
        $id = $this->_getContentParams('id');
        _toValidate("id",$id,20008);
        $data = ModelWithdrawLog::getInstance()->toFind("id=$id",'id,withdraw_number AS withdrawNumber,money,bank_name AS bankName,real_name AS realName,bank_card_number AS bankCardNumber,gmt_create AS gmtCreate,state,gmt_modified AS gmtModified');
        $data['way']="银行卡";
        _returnData($data);
    }

    /**
     * 押金列表
     * uri:/admin/deposit.list
     */
    public function depositList(){
        $account = $this->_getContentParams('account');
        $startTime = $this->_getContentParams('startTime');
        $endTime = $this->_getContentParams('endTime');
        $scope = $this->_getContentParams('scope');
        $arr_where='apply_for_settled.settle_type=3 or state=1';
        switch ($scope){
            case 1://100元以下
                $arr_where.=" and money<=100";
                break;
            case 2://100-200
                $arr_where.=" and money between 100 and 200";
                break;
            case 3://200-500
                $arr_where.=" and money between 200 and 500";
                break;
            case 4://500-1000
                $arr_where.=" and money between 500 and 1000";
                break;
            case 5://1000以上
                $arr_where.=" and money>1000";
                break;
            default:
                break;
        }
        if (!empty($account)){
            $arr_where.=" and apply_for_settled.mobile like %$account%";
        }
        if (!empty($startTime)){
            $arr_where.=" and DATE_FORMAT(apply_for_settled.gmt_modified,'%Y%m%d') >=DATE_FORMAT('$startTime','%Y%m%d')";
        }
        if (!empty($endTime)){
            $arr_where.=" and DATE_FORMAT(apply_for_settled.gmt_modified,'%Y%m%d') <=DATE_FORMAT('$endTime','%Y%m%d')";
        }
        $list = ModelApplyForSettled::getInstance()->toSelect($arr_where,"id,map_id AS mapId,mobile,type,amount,state,gmt_modified AS gmtModified");
        foreach ($list as $key=>$val){

        }
        _returnData($list);
    }
}